import { createRouter, createWebHistory } from "vue-router";
import Login from "@/views/Login/index.vue";
import Home from "@/layout/index.vue";
import { useUserStore } from "@/stores/UserStore";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "index",
      component: Home,
    },
    {
      path: "/login",
      name: "login",
      component: Login,
    },
  ],
});
router.beforeEach((to, from, next) => {
  if (to.path !== "/login") {
    const store = useUserStore();
    if (!store.user || !store.user.token) {
      router.push("/login");
      return;
    }
  }
  next();
});

export default router;
